//= require <configurable>
Tooltip = Class.create(Configurable, {
    body_node_ : '',
    arr_tooltip_links_ : '',
    link_cnt_ : 0,
    initialize: function(options) 
    {
        this.setOptions(options);
        $$('body').each(function(body){
            body.writeAttribute({'role': 'application'});
            this.body_node_ = body;
        }.bind(this));
        this.arr_tooltip_links_ = $$('.'+this.options.class_name+'[title]'); 
        this.arr_tooltip_links_.each(function(link){
            this.body_node_.insert('<div id="tooltip-id-' + ++this.link_cnt_ + '" class="tooltip-detail" aria-hidden="true" role="tooltip" style="display: none;">' + link.readAttribute('title') + '</div>');
            link.writeAttribute({'title' : '', 'aria-describedby' : 'tooltip-id-' + this.link_cnt_});
        }.bind(this));
        if(this.arr_tooltip_links_.length > 0)
        {
            this.body_node_.observe('mouseover', this.showTooltip.bindAsEventListener(this));
            this.body_node_.observe('mouseout', this.hideTooltip.bindAsEventListener(this));
        }
    },
    showTooltip : function(event)
    {
        var element = event.findElement(this.options.tooltip_element);
        if(element == null || element == document)
            return false;
        if(!element.hasAttribute('aria-describedby'))
            return false;
        var div = $(element.readAttribute('aria-describedby'));
        div.setStyle({'left' : (event.pointerX()+20) + 'px', 'top' : (event.pointerY()+15) + 'px'}).writeAttribute({'aria-hidden': 'false'}).show();
    },
    hideTooltip : function(event)
    {
        var link = event.findElement(this.options.tooltip_element);
        if(link == null || link == document)
            return false;
        if(!link.hasAttribute('aria-describedby'))
            return false;
        $(link.readAttribute('aria-describedby')).writeAttribute({'aria-hidden': 'true'}).hide();
    }
});


Tooltip.DEFAULT_OPTIONS = {
    class_name: 'tooltip',
    tooltip_element: 'a'
};
